home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / pluginy Firefox / 6647 / 6647.xpi / chrome / httpfox.jar / content / HttpFoxTree.js < prev    next >
Text File  |  2009-04-10  |  11KB  |  436 lines

  1. /*
  2.     HttpFox - An HTTP analyzer addon for Firefox
  3.     Copyright (C) 2008 Martin Theimer
  4.     
  5.     This program is free software; you can redistribute it and/or modify
  6.     it under the terms of the GNU General Public License as published by
  7.     the Free Software Foundation; either version 2 of the License, or
  8.     (at your option) any later version.
  9.     
  10.     This program is distributed in the hope that it will be useful,
  11.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13.     GNU General Public License for more details.
  14.     
  15.     You should have received a copy of the GNU General Public License
  16.     along with this program; if not, write to the Free Software
  17.     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  18. */
  19.  
  20. // tree implementation for request/response output on main window
  21. function HttpFoxTree(treeElement, HttpFoxControllerReference)
  22. {
  23.    this.init(treeElement, HttpFoxControllerReference);
  24. }
  25.  
  26. HttpFoxTree.prototype = 
  27. {
  28.     HttpFox: null,
  29.     TreeElement: null,
  30.     //treebox: null,
  31.     selection: null,
  32.     
  33.     init: function(treeElement, HttpFoxControllerReference)
  34.     {
  35.         this.HttpFox = HttpFoxControllerReference;
  36.         
  37.         this.TreeElement = treeElement;
  38.         this.TreeElement.view = this;
  39.     },
  40.     
  41.     get rowCount() 
  42.     {
  43.         return this.HttpFox.FilteredRequests.length;
  44.         //return this.HttpFox.Requests.length;
  45.     },
  46.    
  47.     /*set data(httpdata) {
  48.         var oldCount = this.rowCount;
  49.         this.sourceData = httpdata;
  50.         //this.filteredData = this.sourceData.requests;
  51.         // hook into the push function of the array
  52.         this.sourceData.tree = this;
  53.         this.sourceData.oldPush = this.sourceData.push;
  54.         this.sourceData.push = myPush;
  55.         //this.filteredData = this.sourceData.requests;
  56.         this.rowCountChanged(0, -oldCount);
  57.         //this.gui.hasData = (this.sourceData.requests.length > 0);
  58.         //this.filter(this.gui.currentFilter);
  59.     },*/ 
  60.    
  61.     getCellText: function(row, column) 
  62.     {
  63.         var request = this.HttpFox.FilteredRequests[row];
  64.         
  65.         if (request)
  66.         {
  67.             // in deer park, the column is actually a tree column, rather than an id
  68.             if (column.id)
  69.             {
  70.                 column = column.id;
  71.             }
  72.             
  73.              switch(column)
  74.              {
  75.                  case "hf_Column_Started":
  76.                     return net.decoded.utils.formatTime(new Date(request.StartTimestamp - this.HttpFox.HttpFoxService.StartTime.getTime()));
  77.                     
  78.                 case "hf_Column_Time":
  79.                     if (!request.IsFinished)
  80.                     {
  81.                         return "*";
  82.                     }
  83.  
  84.                     return net.decoded.utils.formatTimeDifference(request.StartTimestamp, request.EndTimestamp);
  85.                     
  86.                 case "hf_Column_Sent":
  87.                     var rString = "";
  88.                     
  89.                     if (request.IsSending)
  90.                     {
  91.                         rString = net.decoded.utils.humanizeSize(request.getBytesSent(), 6) + "/" + net.decoded.utils.humanizeSize(request.getBytesSentTotal(), 6);
  92.                     }
  93.                     else
  94.                     {
  95.                         rString = net.decoded.utils.humanizeSize(request.getBytesSentTotal(), 6);    
  96.                     }
  97.                     
  98.                     return rString;
  99.                     
  100.                 case "hf_Column_Received":
  101.                     var rString = "";
  102.                     
  103.                     /*if (request.IsAborted)
  104.                     {
  105.                         return rString;
  106.                     }*/
  107.                     
  108.                     if (request.IsSending)
  109.                     {
  110.                         return "*";
  111.                     }
  112.                     
  113.                     if (!request.IsFinished)
  114.                     {
  115.                         // show loading body progress
  116.                         rString = net.decoded.utils.humanizeSize(request.getBytesLoaded(), 6) + "/" + net.decoded.utils.humanizeSize(request.getBytesLoadedTotal(), 6);
  117.                     }
  118.                     else
  119.                     {
  120.                         rString = net.decoded.utils.humanizeSize(request.getBytesLoaded(), 6);    
  121.                     }
  122.                     
  123.                     if (request.IsFromCache || request.ResponseStatus == 304)
  124.                     {
  125.                         rString = "(" + rString + ")";
  126.                     }
  127.                     
  128.                     return rString;
  129.                     
  130.                 case "hf_Column_Method":
  131.                     return request.RequestMethod;
  132.                     
  133.                 case "hf_Column_Result":
  134.                     if (request.IsAborted)
  135.                     {
  136.                         return "(Aborted)";
  137.                     }
  138.                     
  139.                     if (request.isError())
  140.                     {
  141.                         return "(Error)";
  142.                     }
  143.                 
  144.                     if (request.IsFromCache && (request.ResponseStatus != 304))
  145.                     {
  146.                         return "(Cache)";
  147.                     }
  148.                     
  149.                     if (!request.HasReceivedResponseHeaders && !request.IsFinal)
  150.                     {
  151.                         return "*";
  152.                     }    
  153.                         
  154.                     return request.ResponseStatus;
  155.                     
  156.                 case "hf_Column_Type":
  157.                     if (request.hasErrorCode())
  158.                     {
  159.                         if (request.ContentType)
  160.                         {
  161.                             return request.ContentType + " (" + net.decoded.utils.nsResultErrors[request.Status.toString(16)] + ")";
  162.                         }
  163.                         
  164.                         return net.decoded.utils.nsResultErrors[request.Status.toString(16)];
  165.                     }
  166.                     
  167.                     if (!request.HasReceivedResponseHeaders && !request.IsFromCache && !request.IsFinal)
  168.                     {
  169.                         return "*";
  170.                     }
  171.                     
  172.                     if (request.isRedirect())
  173.                     {
  174.                         if (request.ResponseHeaders && request.ResponseHeaders["Location"])
  175.                         {
  176.                             return "Redirect to: " + request.ResponseHeaders["Location"];    
  177.                         }
  178.                         return "Redirect (cached)";
  179.                     }
  180.                     
  181.                     return request.ContentType;
  182.                     
  183.                 case "hf_Column_URL":
  184.                     return request.Url;
  185.                     
  186.                 default:
  187.                     return "bad column: " + column;
  188.             }
  189.         }
  190.         else
  191.         {
  192.             return "Bad row: " + row;
  193.         }
  194.     },
  195.  
  196.     setTree: function(treebox)
  197.     { 
  198.         this.treebox = treebox; 
  199.     },
  200.  
  201.     isContainer: function(row) 
  202.     {
  203.         return false; 
  204.     },
  205.     isSeparator: function(row) 
  206.     { 
  207.         return false; 
  208.     },
  209.     isSorted: function(row) 
  210.     { 
  211.         return false; 
  212.     },
  213.     getLevel: function(row)
  214.     { 
  215.         return 0; 
  216.     },
  217.     getImageSrc: function(row, col)
  218.     { 
  219.         return null; 
  220.     },
  221.     
  222.     getRowProperties: function(row, props) 
  223.     {
  224.         var aserv = Components.classes["@mozilla.org/atom-service;1"].getService(Components.interfaces.nsIAtomService);
  225.         var request = this.HttpFox.FilteredRequests[row];
  226.         
  227.         if (this.TreeElement.currentIndex == row) 
  228.         {
  229.             props.AppendElement(aserv.getAtom("hf_currentRow"));
  230.         }
  231.         
  232.         if (this.TreeElement.view.selection.isSelected(row)) 
  233.         {
  234.             return;
  235.         }
  236.         
  237.         if (!this.HttpFox.HttpFoxService.Preferences.ColorRequests)
  238.         {
  239.             return;
  240.         }
  241.         
  242.         if (request.isHTTPS()) 
  243.         {
  244.             props.AppendElement(aserv.getAtom("hf_HTTPS"));
  245.             //return;
  246.         }
  247.         
  248.         if (request.IsFromCache || request.ResponseStatus == 304) 
  249.         {
  250.             props.AppendElement(aserv.getAtom("hf_fromCache"));
  251.             return;
  252.         }
  253.         
  254.         if (request.isRedirect()) 
  255.         {
  256.             props.AppendElement(aserv.getAtom("hf_isRedirect"));
  257.             return;
  258.         }
  259.         
  260.         if (request.isError()) 
  261.         {
  262.             props.AppendElement(aserv.getAtom("hf_isError"));
  263.             return;
  264.         }
  265.         
  266.         if (request.hasErrorCode() || request.ResponseStatus >= 400) 
  267.         {
  268.             props.AppendElement(aserv.getAtom("hf_hasError"));
  269.             return;
  270.         }
  271.         
  272.         if (request.IsFinished && request.ResponseStatus == 200) 
  273.         {
  274.             props.AppendElement(aserv.getAtom("hf_OK"));
  275.             //return;
  276.         }
  277.         
  278.     },
  279.     
  280.     getCellProperties: function(row, col, props)
  281.     {
  282.         var aserv = Components.classes["@mozilla.org/atom-service;1"].getService(Components.interfaces.nsIAtomService);
  283.         var request = this.HttpFox.FilteredRequests[row];
  284.         
  285.         if (this.TreeElement.view.selection.isSelected(row)) 
  286.         {
  287.             return;
  288.         }
  289.         
  290.         if (!this.HttpFox.HttpFoxService.Preferences.ColorRequests)
  291.         {
  292.             return;
  293.         }
  294.         
  295.         if (request.IsFromCache) 
  296.         {
  297.             props.AppendElement(aserv.getAtom("hf_fromCache"));
  298.             //return;
  299.         }
  300.         
  301.         if (request.isError()) 
  302.         {
  303.             props.AppendElement(aserv.getAtom("hf_isError"));
  304.             return;
  305.         }
  306.         
  307.         if (request.isHTTPS()) 
  308.         {
  309.             props.AppendElement(aserv.getAtom("hf_HTTPS"));
  310.             //return;
  311.         }
  312.     },
  313.  
  314.     getColumnProperties: function(colid, col, props)
  315.     {
  316.     },
  317.  
  318.     rowCountChanged: function(index, count) 
  319.     {
  320.         //dump('\nROWCOUNT CHANGED (' + index + ', ' + count + ')\n');
  321.         //alert(this.treebox);
  322.         if (this.treebox) 
  323.         {
  324.             var lvr = this.treebox.getLastVisibleRow();
  325.             this.treebox.rowCountChanged(index, count);
  326.             // If the last line of the tree is visible on screen, we will autoscroll
  327.             //if ((lvr + 1) >= index || this.HttpFox.isAutoScroll())
  328.             if (this.HttpFox.isAutoScroll())
  329.             {
  330.                 this.treebox.ensureRowIsVisible(this.rowCount - 1);
  331.             }
  332.         }
  333.         if (this.rowCount > 0) 
  334.         {
  335.             //this.gui.hasVisibleData = true;
  336.         } 
  337.         else 
  338.         {
  339.             //this.gui.hasVisibleData = false;
  340.         }
  341.     },
  342.  
  343.     invalidateRow: function(index) 
  344.     {
  345.         if (this.treebox) 
  346.         {
  347.             this.treebox.invalidateRow(index);
  348.         }
  349.     },
  350.     
  351.     invalidate: function()
  352.     {
  353.         this.treebox.invalidate();
  354.     },
  355.  
  356.     getCurrent: function() 
  357.     {
  358.         if (this.HttpFox.FilteredRequests[this.TreeElement.currentIndex]) 
  359.         {
  360.             return this.HttpFox.FilteredRequests[this.TreeElement.currentIndex];
  361.         } 
  362.         else
  363.         {
  364.             return null;
  365.         }
  366.     },
  367.     
  368.     setCurrent: function(index)
  369.     {
  370.         this.TreeElement.currentIndex = index;
  371.         this.invalidate();
  372.     },
  373.     
  374.     setCurrentToNewest: function()
  375.     {
  376.         this.TreeElement.currentIndex = this.rowCount;
  377.         this.invalidate();
  378.     }
  379.  
  380.    /*getText : function(all) {
  381.       var text = "";
  382.       var start = 0;
  383.       var end = 0;
  384.       if (all) {
  385.          end = this.filteredData.length;
  386.          for (var current = start; current < end; current++) {
  387.             if (current > start) {
  388.                text += "\n";
  389.             }
  390.             text += this.filteredData[current].toString() + "\n";
  391.          }
  392.       } else {
  393.          // do some tricky stuff here to retrieve all of the
  394.          // selected rows
  395.          var rangeStart = new Object();
  396.          var rangeEnd = new Object();
  397.          var numRanges = this.tree.view.selection.getRangeCount();
  398.          
  399.          for (var t = 0; t < numRanges; t++){
  400.             this.tree.view.selection.getRangeAt(t , rangeStart, rangeEnd);
  401.             for (var v = rangeStart.value; v <= rangeEnd.value; v++){
  402.                // now we've got the index ...
  403.                if (t != 0 || v != 0) {
  404.                   text += "\n";
  405.                }
  406.                text += this.filteredData[v].toString() + "\n";
  407.             }
  408.          }
  409.       }
  410.       return text;
  411.    },*/
  412.  
  413.   /* filter : function(value) {
  414.       var oldRowCount = this.rowCount;
  415.       if (value == null || value == "") {
  416.          this.filteredData = this.sourceData.requests;
  417.       } else {
  418.          value = value.toLowerCase();
  419.          this.filteredData = new Array();
  420.          var request = null;
  421.          for (var i = 0; i < this.sourceData.requests.length; i++) {
  422.             request = this.sourceData.requests[i];
  423.             if (request.uri.toLowerCase().indexOf(value) >= 0) {
  424.             // make it a preference to filter on uri v.s. whole string
  425.             // if (request.toString().toLowerCase().indexOf(value) >= 0) {
  426.                this.filteredData.push(request);
  427.             }
  428.          }
  429.       }
  430.       if (this.treebox) {
  431.          this.treebox.invalidate();
  432.          this.rowCountChanged(0, this.rowCount - oldRowCount);
  433.       }
  434.    }*/
  435.      
  436. }